package com.zzj.ecology.system.service.impl.system;

import com.zzj.ecology.common.constant.GlobalConstant;
import com.zzj.ecology.system.bean.system.SysDept;
import com.zzj.ecology.system.bean.system.SysDeptExample;
import com.zzj.ecology.system.dto.HttpResult;
import com.zzj.ecology.system.domain.Page;
import com.zzj.ecology.system.bean.vo.dtree.DTreeResponse;
import com.zzj.ecology.system.mapper.system.SysDeptMapper;
import com.zzj.ecology.system.service.system.SysDeptService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName SysDeptServiceImpl
 * @Description TODO
 * @Author zhijun zeng at 23:05 2020/4/9
 * @Version 1.0
 **/
@Service("sysDeptService")
@Transactional(rollbackFor = Exception.class)
public class SysDeptServiceImpl implements SysDeptService {

    public static final Logger logger = LoggerFactory.getLogger(SysDeptServiceImpl.class);

    @Autowired
    private SysDeptMapper sysDeptMapper;


    /**
     * 分页 查询部门列表数据
     * @param page
     * @return
     */
    public HttpResult findPageList(Page page) {
        int currentPage = page.getCurrentPage();
        Integer pageCounts = page.getPageCounts();
        int first = (currentPage - 1) * pageCounts;

        List<SysDept> lists = sysDeptMapper.findPageList(first, pageCounts);
        page.getData().put(HttpResult.LIST_TAG, lists);// 当前页面数据

        SysDeptExample example = new SysDeptExample();
        SysDeptExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteStatusEqualTo(GlobalConstant.DELETE_STATUS_NOT_DELETE);//delete_status='0' 的数据

        List<SysDept> list1 = sysDeptMapper.selectByExample(example);//不分页总数据
        page.setTotalCounts(list1.size());
        return HttpResult.success().add(HttpResult.PAGE_TAG, page);
    }

    /**
     * 查询 下拉框 数据,数据格式
     * @return
     */
    public DTreeResponse dtree() {
        List<SysDept> depts = sysDeptMapper.dtree();
        DTreeResponse dTreeResponse = new DTreeResponse();
        dTreeResponse.setData(depts);
        return  dTreeResponse;
    }

    /**
     * 根据 部门名称 查询部门名称是否已经存在. 存在返回true, 不存在返回 false
     * @param deptName
     * @return
     */
    public boolean checkExists(String deptName) {
        SysDeptExample example = new SysDeptExample();
        example.createCriteria().andDeptNameEqualTo(deptName);
        List<SysDept> depts = sysDeptMapper.selectByExample(example);
        return depts.size() > 0 ? true : false;
    }

    /**
     * 创建一条新的部门记录
     * @param sysDept
     * @return
     */
    public HttpResult create(SysDept sysDept) {
        if (sysDept.getParentId() != null && sysDept.getParentId() != 0) {
            SysDept pDept = sysDeptMapper.selectByPrimaryKey(sysDept.getParentId());
            sysDept.setAncestors(pDept.getAncestors() + "," + pDept.getDeptId());
        }
        int i = sysDeptMapper.insertSelective(sysDept);
        return i > 0 ? HttpResult.success() : HttpResult.error();//当插入记录更新时,返回成功.如果失败直接报错.
    }

    /**
     * 根据部门ID 修改部门信息
     * @param sysDept
     * @return
     */
    public HttpResult update(SysDept sysDept) {
        if (sysDept.getParentId() != null && sysDept.getParentId() != 0) {
            SysDept pDept = sysDeptMapper.selectByPrimaryKey(sysDept.getParentId());
            sysDept.setAncestors(pDept.getAncestors() + "," + pDept.getDeptId());
        }
        int i = sysDeptMapper.updateByPrimaryKeySelective(sysDept);
        return i > 0 ? HttpResult.success() : HttpResult.error();//当修改记录更新时,返回成功.如果失败直接报错.
    }

    /**
     * 批量修改 delete_status = '0' 修改成 '1', 记录修改人员并且修改时间
     * @param ids
     * @param username
     * @return
     */
    public HttpResult updateDeleteStatusByIds(List<Integer> ids, String username) {
        sysDeptMapper.updateDeleteStatusByIds(ids, username);
        return HttpResult.success();
    }
}
